<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/animate.js"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    li {
      list-style: none;
    }

    img {
      vertical-align: top;
    }

    .wrap {
      width: 1226px;
      height: 460px;
      margin: 0 auto;
      margin-top: 100px;
      position: relative;
    }

    .swiper {
      width: 100%;
    }

    .swiperBar {
      /* 父元素的宽度  == 所有子元素的宽度和 */
      display: flex;
      width: max-content;
      height: 460px;
      position: absolute;
      left: 0;
      top: 0;
    }

    .swiperBar li {
      float: left;
    }

    .nav {
      position: absolute;
      right: 50px;
      bottom: 20px;
      display: flex;
    }

    .nav li {
      width: 15px;
      height: 15px;
      background-color: #fff;
      margin: 5px;
      border-radius: 50%;
    }

    .nav .active {
      background-color: blue;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="swiper">
      <ul class="swiperBar">
        <div><img src="../images/1.jpg" alt=""></div>
        <div><img src="../images/2.jpg" alt=""></div>
        <div><img src="../images/3.jpg" alt=""></div>
        <div><img src="../images/4.jpg" alt=""></div>
        <div><img src="../images/1.jpg" alt=""></div>
      </ul>
    </div>
    <ul class="nav">
      <li class="active"></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>
</body>
</body>
<script>
  // 轮播图的书写流程
  // 1.  dot点 和 图片 一一对应 (下标)
  // 2.  怎么移动? 轮播时 移动移动整个父盒子(swiperBar)  => 无缝轮播
  // 3.  移动多少? swiperBar移动的距离是一张图片的倍数 => 具体移动多少也和下标对应
  var swiper = document.querySelector('.swiper');
  var swiperBar = document.querySelector('.swiperBar');
  var liList = document.querySelectorAll('.nav li');


  var swiperWidth = swiper.clientWidth;
  var timer = null;
  var index = 0;  //全局变量  记录轮播的下标

  autoPlay(); //页面加载时 开启自动轮播

  // 点击轮播
  for (let i = 0; i < liList.length; i++) {
    let li = liList[i];
    li.onclick = function () {
      console.log(i);
      index = i; // 记录点击的位置 => 如果自动轮播从当前位置开始
      for (var j = 0; j < liList.length; j++) {
        liList[j].className = '';
      }
      liList[i].className = 'active';
      animate(swiperBar, "left", -swiperWidth * i, "normal");
    }
  }

  //移进暂停
  swiper.onmouseenter = function () {
    clearInterval(timer);
  }

  //移出开始
  swiper.onmouseleave = function () {
    autoPlay();
  }

  //自动轮播
  function autoPlay() {
    timer = setInterval(function () {
      index++;
      for (var j = 0; j < liList.length; j++) {
        liList[j].className = '';
      }
      liList[index >= liList.length ? 0 : index].className = 'active';
      animate(swiperBar, "left", -swiperWidth * index, "normal", function () {
        // 此回调函数 每次运动结束之后执行
        // 判断 如果是第五张 瞬间切换到第一张
        console.log('每次运动结束之后执行:', index);
        if (index >= liList.length) {
          swiperBar.style.left = 0;
          index = 0;
        }
      });

    }, 3000)
  }



</script>

</html>